package org.dromara.learning_lhz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.learning_lhz.entity.AsLearningTasksEntity;
import org.dromara.learning_lhz.entity.AsStudentAdminEntity;
import org.dromara.learning_lhz.entity.dto.AsLearningTasksDto;
import org.dromara.learning_lhz.entity.vo.TaskVo;
import org.dromara.learning_lhz.mapper.LearningTasksMapper;
import org.dromara.learning_lhz.service.LearningTasksService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LearningTasksServiceImpl extends ServiceImpl<LearningTasksMapper, AsLearningTasksEntity> implements LearningTasksService {

    @Resource
    protected LearningTasksMapper learningTasksMapper;

    @Override
    public Page<TaskVo> selTask(PageQuery pageQuery, AsLearningTasksDto asLearningTasksDto) {
        MPJLambdaWrapper<AsLearningTasksEntity> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(Objects.nonNull(asLearningTasksDto.getId()), AsLearningTasksEntity::getId, asLearningTasksDto.getId())
            .select(AsLearningTasksEntity::getId, AsLearningTasksEntity::getTaskName, AsLearningTasksEntity::getSubject, AsLearningTasksEntity::getKnowledgePoints, AsLearningTasksEntity::getState, AsLearningTasksEntity::getTimeStart, AsLearningTasksEntity::getTimeEnd)
            .select(AsStudentAdminEntity::getStudentName)
            .leftJoin(AsStudentAdminEntity.class, AsStudentAdminEntity::getId, AsLearningTasksEntity::getPId);

        // 根据学生姓名模糊查询
        if (asLearningTasksDto.getStudentName() != null) {
            wrapper.like(AsStudentAdminEntity::getStudentName, asLearningTasksDto.getStudentName());
        }

        // 执行分页查询
        Page<TaskVo> studyPlanVoPage = learningTasksMapper.selectJoinPage(pageQuery.build(), TaskVo.class, wrapper);

        // 结果合并
        Map<String, TaskVo> mergedResults = new LinkedHashMap<>();

        for (TaskVo taskVo : studyPlanVoPage.getRecords()) {
            // 用 taskName 和 knowledgePoints 作为唯一键来进行合并
            String key = taskVo.getTaskName() + "_" + taskVo.getKnowledgePoints();

            if (mergedResults.containsKey(key)) {
                // 如果已经存在，合并 studentName 到集合
                TaskVo existingVo = mergedResults.get(key);
                existingVo.getStudentNames().add(taskVo.getStudentName());
            } else {
                // 如果不存在，初始化新的 TaskVo，并将所有字段赋值
                TaskVo newVo = new TaskVo();
                newVo.setId(taskVo.getId());  // 设置 id
                newVo.setTaskName(taskVo.getTaskName());
                newVo.setKnowledgePoints(taskVo.getKnowledgePoints());
                newVo.setSubject(taskVo.getSubject());
                newVo.setState(taskVo.getState());
                newVo.setTimeStart(taskVo.getTimeStart());
                newVo.setTimeEnd(taskVo.getTimeEnd());
                newVo.setStudentNames(new ArrayList<>());
                newVo.getStudentNames().add(taskVo.getStudentName());  // 设置 studentName

                mergedResults.put(key, newVo);
            }
        }

        // 创建新的分页对象
        Page<TaskVo> resultPage = new Page<>(studyPlanVoPage.getCurrent(), studyPlanVoPage.getSize(), mergedResults.size());
        resultPage.setRecords(new ArrayList<>(mergedResults.values()));

        return resultPage;
    }

    @Override
    public void addTask(AsLearningTasksDto asLearningTasksDto) {
        // 遍历 studentId 列表并批量插入记录
        List<AsLearningTasksEntity> planEntities = new ArrayList<>();

        for (Long studentId : asLearningTasksDto.getStudentId()) {
            AsLearningTasksEntity plan = new AsLearningTasksEntity();
            plan.setSubject(asLearningTasksDto.getSubject());
            plan.setTaskName(asLearningTasksDto.getTaskName());
            plan.setKnowledgePoints(asLearningTasksDto.getKnowledgePoints());
            plan.setState(asLearningTasksDto.getState());
            plan.setTimeStart(asLearningTasksDto.getTimeStart());
            plan.setTimeEnd(asLearningTasksDto.getTimeEnd());
            plan.setPId(studentId);
            planEntities.add(plan);
        }
        // 使用 MyBatis-Plus 的批量插入功能
        if (!planEntities.isEmpty()) {
            this.saveBatch(planEntities); // 请确保你的 Mapper 支持批量插入
        }
    }
}
